package com.ktwlsoft.fundmanage.controller;

import com.ktwlsoft.fundmanage.domain.entity.pictureconfig.SignCredential;
import com.ktwlsoft.fundmanage.domain.entity.pictureconfig.SignCredentialInfo;
import com.ktwlsoft.fundmanage.service.pictureconfig.SignCredentialInfoService;
import com.ktwlsoft.fundmanage.service.pictureconfig.SignCredentialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * 通用图片控制器
 *
 * @author zhuhe on 2019-07-17 11:07:06
 * @version 1.0
 */
@Api(description = "图片控制器")
@RestController
@RequestMapping("/files")
public class FileController {

	/**要件业务层*/
	private final SignCredentialService signCredentialService;

	/**要件详情业务层*/
	private final SignCredentialInfoService signCredentialInfoService;

	public FileController(SignCredentialService signCredentialService, SignCredentialInfoService signCredentialInfoService) {
		this.signCredentialService = signCredentialService;
		this.signCredentialInfoService = signCredentialInfoService;
	}

	/**
	 * 要件修改 on 2019-03-04 15:53:17
	 *
	 * @param signCredential signCredential
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "要件修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@PostMapping(value = "signCredentialUpdate")
	public ResponseEntity<?> signCredentialUpdate(@RequestBody SignCredential signCredential) {
		return ResponseEntity.ok(signCredentialService.signCredentialUpdate(signCredential));
	}

	/**
	 * 要件删除 on 2019/8/15 18:35
	 *
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "要件删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("signCredentialDelete")
	public ResponseEntity<?> signCredentialDelete(@ApiParam(name = "credentialId", value = "主键") @RequestParam(defaultValue = "") String credentialId){
		return ResponseEntity.ok(signCredentialService.signCredentialDelete(credentialId));
	}

	/**
	 * 要件分页 on 2019/8/15 18:35
	 *
	 * @return payinTypePage<?>
	 */
	@ApiOperation(value = "要件分页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("signCredentialPage")
	public ResponseEntity<?> signCredentialPage(@RequestParam(defaultValue = "1") int pn,
												@RequestParam(defaultValue = "30") int pageSize,
												@ApiParam(name = "parentId", value = "父级id") @RequestParam(defaultValue = "") String parentId,
												@ApiParam(name = "keyWords", value = "关键字") @RequestParam(defaultValue = "") String keyWords){
		return ResponseEntity.ok(signCredentialService.signCredentialPage(pn,pageSize,parentId,keyWords));
	}

	/**
	 * 要件详情 on 2019/8/15 18:35
	 *
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "要件详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("signCredentialDetails")
	public ResponseEntity<?> signCredentialDetails(@ApiParam(name = "credentialId", value = "主键") @RequestParam(defaultValue = "") String credentialId){
		return ResponseEntity.ok(signCredentialService.signCredentialDetails(credentialId));
	}

	/**
	 * 要件详情修改 on 2019-03-04 15:53:17
	 *
	 * @param signCredentialInfo signCredentialInfo
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "要件详情修改", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@PostMapping(value = "signCredentialInfoUpdate")
	public ResponseEntity<?> signCredentialInfoUpdate(@RequestBody SignCredentialInfo signCredentialInfo) {
		return ResponseEntity.ok(signCredentialInfoService.signCredentialInfoUpdate(signCredentialInfo));
	}

	/**
	 * 要件详情删除 on 2019/8/15 18:35
	 *
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "要件详情删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("signCredentialInfoDelete")
	public ResponseEntity<?> signCredentialInfoDelete(@ApiParam(name = "credentialInfoId", value = "主键") @RequestParam(defaultValue = "") String credentialInfoId){
		return ResponseEntity.ok(signCredentialInfoService.signCredentialInfoDelete(credentialInfoId));
	}

	/**
	 * 要件详情分页 on 2019/8/15 18:35
	 *
	 * @return payinTypePage<?>
	 */
	@ApiOperation(value = "要件详情分页", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("signCredentialInfoPage")
	public ResponseEntity<?> signCredentialInfoPage(@RequestParam(defaultValue = "1") int pn,
													@RequestParam(defaultValue = "30") int pageSize,
													@ApiParam(name = "keyWords", value = "关键字") @RequestParam(defaultValue = "") String keyWords,
													@ApiParam(name = "credentialId", value = "要件id") @RequestParam(defaultValue = "") String credentialId){
		return ResponseEntity.ok(signCredentialInfoService.signCredentialInfoPage(pn,pageSize,keyWords,credentialId));
	}

	/**
	 * 要件详情详情 on 2019/8/15 18:35
	 *
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "要件详情详情", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("signCredentialInfoDetails")
	public ResponseEntity<?> signCredentialInfoDetails(@ApiParam(name = "credentialInfoId", value = "主键") @RequestParam(defaultValue = "") String credentialInfoId){
		return ResponseEntity.ok(signCredentialInfoService.signCredentialInfoDetails(credentialInfoId));
	}

	/**
	 * 通过busiId查询要件及要件详情数据 on 2019/8/15 18:35
	 *
	 * @return ResponseEntity<?>
	 */
	@ApiOperation(value = "通过busiId查询要件及要件详情数据", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
	@ResponseBody
	@GetMapping("findByBusiId")
	public ResponseEntity<?> findByBusiId(@ApiParam(name = "busiId", value = "业务id") @RequestParam(defaultValue = "") String busiId,
										  @ApiParam(name = "busiCode", value = "所属业务功能模块代码") @RequestParam(defaultValue = "") String busiCode){
		return ResponseEntity.ok(signCredentialInfoService.findByBusiId(busiId,busiCode));
	}

//	/**图片上传*/
//	@Autowired
//	private FileStorageService fileStorageService;
//
//	/**
//	 * 通用图片上传 on 2019-03-04 15:53:17
//	 *
//	 * @param request request
//	 * @return ResponseEntity<?>
//	 */
//	@ApiOperation(value = "通用图片上传", httpMethod = "POST", produces = "application/json", response = ResponseEntity.class)
//	@ResponseBody
//	@PostMapping(value = "imgUpload")
//	public ResponseEntity<?> imgUpload(HttpServletRequest request) {
//		// 获取文件集合
//		List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("files");
//		// 交易编号
//		String tsCode = request.getParameter("tsCode");
//		if (files.size() <= 0) {
//			return ResponseEntity.ok(BaseResult.body(null,ResultType.SAVING_ERROR).withMessage("文件为空"));
//		}
//		return imgUpload(files,tsCode);
//	}
//
//	/**
//	 * 文件预览 on 2019/3/12 0012 16:36
//	 *
//	 * @return ResponseEntity<?>
//	 */
//	@ApiOperation(value = "文件预览", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
//	@GetMapping("getFileView")
//	public ResponseEntity<?> getFileView(String url, HttpServletRequest request) {
//		if (url == null || "".equals(url.trim())) {
//			return null;
//		}
//		// Load file as Resource
//		Resource resource = fileStorageService.loadFileAsResource(url);
//		// Try to determine file's content type
//		String contentType = null;
//		try {
//			contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
//		} catch (IOException ex) {
//			logger.info("Could not determine file type.");
//		}
//		// Fallback to the default content type if type could not be determined
//		if (contentType == null) {
//			contentType = "application/octet-stream";
//		}
//		return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType)).body(resource);
//	}
//
//	/**
//	 * 文件下载 on 2019/3/12 0012 16:36
//	 *
//	 * @return ResponseEntity<Resource>
//	 */
//	@ApiOperation(value = "文件下载", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
//	@GetMapping("getFile")
//	public ResponseEntity<Resource> getFile(String url, HttpServletRequest request) {
//		if (url == null || "".equals(url.trim())) {
//			return null;
//		}
//		// Load file as Resource
//		Resource resource = fileStorageService.loadFileAsResource(url);
//		// Try to determine file's content type
//		String contentType = null;
//		try {
//			contentType = request.getServletContext().getMimeType(resource.getFile().getAbsolutePath());
//		} catch (IOException ex) {
//			logger.info("Could not determine file type.");
//		}
//		// Fallback to the default content type if type could not be determined
//		if (contentType == null) {
//			contentType = "application/octet-stream";
//		}
//		return ResponseEntity.ok().contentType(MediaType.parseMediaType(contentType))
//				.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
//				.body(resource);
//	}
//
//	/**
//	 * 物理删除 on 2019-07-17 11:07:06
//	 * @param url	url
//	 * @return ResponseEntity<?>
//	 */
//	@ApiOperation(value = "物理删除", httpMethod = "GET", produces = "application/json", response = ResponseEntity.class)
//	@ResponseBody
//	@GetMapping(value = "deleteByUrl")
//	public ResponseEntity<?> deleteByUrl(String url){
//        if(url == null || "".equals(url.trim())){
//			return ResponseEntity.ok(BaseResult.body(null,ResultType.BUSINESS_ERROR).withMessage("url为空"));
//		}
//		try {
//			FileUtils.deleteFile(fileStorageService.getStrPath(url));
//		}catch (Exception e){
//			e.printStackTrace();
//			return ResponseEntity.ok(BaseResult.body(null,ResultType.DELETE_ERROR));
//		}
//		return ResponseEntity.ok(BaseResult.body(url,ResultType.BUSINESS_SUCCESS));
//	}
//
//	/**
//	  * 图片上传 on 2019/7/17 14:52
//	  *
//	  * @param files files
//	  * @param tsCode tsCode
//	  * @return ResponseEntity<?>
//	  */
//	private ResponseEntity<?> imgUpload(List<MultipartFile> files, String tsCode){
//		if (files.size() <= 0) {
//			return ResponseEntity.ok(BaseResult.body(null,ResultType.BUSINESS_ERROR).withMessage("文件为空"));
//		}
//		if (tsCode == null || "".equals(tsCode.trim())) {
//			return ResponseEntity.ok(BaseResult.body(null,ResultType.BUSINESS_ERROR).withMessage("交易编号为空"));
//		}
//		//查询是否有过往数据
//		String path = DateUtils.dateToStr(new Date(),"yyyy") + File.separator + tsCode;
//		List<String> arrayList = Lists.newArrayList();
//		for (MultipartFile value : files) {
//			String fileName = fileStorageService.storeFile(value, path);
//			arrayList.add(path + File.separator + fileName);
//		}
//		return ResponseEntity.ok(BaseResult.body(arrayList,ResultType.BUSINESS_SUCCESS));
//	}
}
